package day9;

import com.sun.xml.internal.bind.v2.model.core.ID;

//通过synchronized模拟多线程轮流并发执行
//假设三个线程A、B、C 要求顺序是ABCABCABC...
//1.可以使用逻辑判断 无需使用notify()和wait()
//缺点：浪费很多次循环 而且代码特别冗余
//2.借助于它wait()和notify()实现
public class Demo5 {
    int n = 1;//1(A) 2(B) 3(C)
    public synchronized void printA(){
        if (n==1){
            System.out.println(Thread.currentThread().getName());
            n = 2;
        }
    }
    public synchronized void printB(){
        if (n==2){
            System.out.println(Thread.currentThread().getName());
            n = 3;
        }
    }
    public synchronized void printC(){
        if (n==3){
            System.out.println(Thread.currentThread().getName());
            n = 1;
        }
    }
    public static void main(String[] args) {
          Demo5 d = new Demo5();
          new Thread(()->{
              while (true) d.printA();
              },"A").start();
          new Thread(()->{
              while (true) d.printB();
          },"B").start();
          new Thread(()->{
              while (true) d.printC();
          },"C").start();
    }
}
